# -*- coding: utf-8 -*-

from odoo import models, fields, api


class WechatUsers(models.Model):
    _description = u'微信用户'
    _name = 'tour.users'
    _inherit = 'res.users'
    _table = 'res_users'

    name = fields.Char('昵称')
    openid = fields.Char('OpenId', index=True)
    unionid = fields.Char('UnionId')
    gender = fields.Integer('gender')
    language = fields.Char('语言')
    register_type = fields.Selection([('app', '微信小程序'),
                                      ('other', '其他')], string='注册来源',
                                     default='app')
    phone = fields.Char('手机号码')
    country = fields.Char('国家')
    province = fields.Char('省份')
    city = fields.Char('城市')
    avatar = fields.Html('头像', compute='_compute_avatar')
    avatar_url = fields.Char('头像链接')
    register_ip = fields.Char('注册IP')
    last_login = fields.Datetime('登陆时间')
    ip = fields.Char('登陆IP')
    role = fields.Selection([('agency', u'旅行社'), ('driver', u'车主')], string=u'角色')

    _sql_constraints = [(
        'tour_user_unionid_unique',
        'UNIQUE (unionid, create_uid)',
        'wechat user openid with create_uid is existed！'
    ),
        (
            'tour_user_openid_unique',
            'UNIQUE (openid, create_uid)',
            'wechat user openid with create_uid is existed！'
        ),
    ]

    @api.multi
    @api.depends('avatar_url')
    def _compute_avatar(self):
        for each_record in self:
            if each_record.avatar_url:
                each_record.avatar = """
                <img src="{avatar_url}" style="max-width:100px;">
                """.format(avatar_url=each_record.avatar_url)
            else:
                each_record.avatar = False
